package de.eventdroid.dao;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;

import java.io.InputStream;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import java.io.IOException;


public class DatenbankInsert extends ListActivity {
	
	JSONArray jArray;
	String result = null;
	InputStream is = null;
	StringBuilder sb=null;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
	}
	
	/**
	 * This Method resolves the users request for new Locations
	 * 
	 * @param view actual View the user clicked on
	 */
	public void onButtonClick(View view) {	
		
		
		
	}
	
	/**
	 * This method writes Data via Post Command to the RESTful Server
	 * 
	 * @param clickedView The view which has been clicked
	 * @return was the writing to the RESTful Service successful
	 * @throws ClientProtocolException Errors within the involved protocols
	 * @throws IOException Error on lower connection layer
	 */
	public boolean writeDataToServer(View clickedView) throws ClientProtocolException, IOException {
		
		boolean transferSuccessfull = false;
		String data = "Data";
		DefaultHttpClient httpclient = new DefaultHttpClient();
		HttpPut httpPut = new HttpPut("http://www.redphoenix.de/eventdroid/getAllLocations.php");
		httpPut.addHeader("Content-Type", "application/xml; charset=utf-8");
		httpPut.addHeader("User-Agent", "Android");
		HttpEntity entity = new StringEntity(data);
		httpPut.setEntity(entity);
		HttpResponse response = httpclient.execute(httpPut);
		HttpEntity input = response.getEntity();
		//StringBuilder result = new StringBuilder();
		if (input != null) {
		    InputStream instream = input.getContent();
//		    result = convertStreamToString(instream);
		    instream.close();
		}
		
		httpclient.getConnectionManager().shutdown();
		
		return transferSuccessfull;
		
	}
	
}
